package com.liuyi.pay.ali;

import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * 支付支付实体填充配置类
 */
@Configuration
public class AliPayConfig {

    private final AliPayBeans aliPayBeans;

    @Autowired
    public AliPayConfig(AliPayBeans aliPayBeans) {
        this.aliPayBeans = aliPayBeans;
        init();
    }

    public void init() {
        if (aliPayBeans != null && aliPayBeans.getBeans() != null) {
            aliPayBeans.getBeans().forEach(item -> {
                AliPayApiConfig config = AliPayApiConfig.builder()
                        .setAppId(item.getAppId())
                        .setAliPayCertPath(item.getAliPayCertPath())
                        .setAliPayPublicKey(item.getPublicKey())
                        .setAliPayRootCertPath(item.getAliPayRootCertPath())
                        .setAppCertPath(item.getAppCertPath())
                        .setPrivateKey(item.getPrivateKey())
                        .setServiceUrl(item.getServerUrl())
                        .setSignType("RSA2")
                        .build();
                AliPayApiConfigKit.putApiConfig(config);
            });
        }
    }
}
